<?php
/**
 * This class supports actions for bed management, ex change beds..
 *
 * @package core.controllers 
 * @author pktquynh
 * @version $Id:$
 * @copyright 2009 LHV Company
 */

$loader->requireOnce('includes/BedManagement/BedManagement.class.php');

class C_BedManagement extends Controller {
    
	var $logger;
	var $template_mod;
	
    public function C_BedManagement($template_mod = "general") {
        parent::Controller();
        $this->logger = &LoggerManager::getLogger('core.controllers.C_BedManagement');
        if (Celini::getCurrentController() == 'BedManagement') {
            $menu =& Menu::getInstance();
            $menu->currentSection = 'admin';
        }
        $this->template_mod = $template_mod;        
        $this->view->path = 'bedmanagement';
    }
	
    /**
     * Return html view to arrange or change bed for the specific patient at some department.
     * @return array('error' => array('code'=>[error code],'mess'=>[error mess]), 'result' => html view)<br>
     * error code=0: This department has no room to change bed for patient
     */
    public function actionArrangeBedForPatient() {
    	$patientId = $_GET['patientId'];
    	$deptId = $_GET['deptId'];
    	$ticketId = $_GET['ticketId'];
    	
    	// get current bed of patient
    	$bedManagement = new BedManagement();
    	$patBed = $bedManagement->getCurrentBedOfPatient($patientId);
    	$patientName = '';
    	$deptName = '';
    	$bedInfo = '';
    	$currentBedId = '';
    	if ($patBed) {
    		$patientName = $patBed['patient_name'];
    		$patientCode = $patBed['patient_code'];
            $deptName = $patBed['department_name'];
            $bedInfo = $patBed['room_name'] . " / " 
                     . " " . $patBed['bed_code'];
            $currentBedId = $patBed['bed_id'];  
    	} else {
    		// patient has no bed
    		$patientName = Celini::newORDO('Person', $patientId)->get('name');    		
    	    $patientCode = Celini::newORDO('Patient', $patientId)->get('patient_code');
			
    	    $dept = Celini::newORDO('Department')->getDepartment($deptId);
    	    $deptName = $dept['name'];
    	    
    	    $bedInfo = L10nHelper::_("Patient hasn't been arranged any bed");
    	}
        // get list of rooms which has at least one bed
        $rooms = $bedManagement->getRoomsAtLeastBed($deptId);
        $roomArray = array();
        foreach ($rooms as $ro) {
        	$roomArray[$ro['room_id']] = $ro['room_name'];
        }
                                
        $this->assign('patientName', $patientName);
        $this->assign('patientCode', $patientCode);
        $this->assign('deptName', $deptName);
        $this->assign('bedInfo', $bedInfo);
    	$this->assign('roomList', $roomArray);
    	// hidden value (instead of storing them in session)
    	$this->assign('patientId', $patientId);
    	$this->assign('deptId', $deptId);
    	$this->assign('ticketId', $ticketId);
    	$this->assign('currentBedId', $currentBedId);
    	
        $errorArr = array('code'=>0,'mess'=>'');
        if (empty($roomArray)) {
            $errorArr['code'] = 1;
            $errorArr['mess'] = L10nHelper::_('This department has no room');
        }
        $result = array('error'  => $errorArr,
                        'result' => $this->view->render('change_bed.html'));
        return json_encode($result);
    }
    
    /**
     * Get list of available beds of a specific room. These beds are arranged for patient. 
     * @return string Html response
     */
    public function actionGetAvailableBedsSelect() {
    	$bedManagement = new BedManagement();
    	$roomId = $_GET['roomId'];
    	if ($roomId == 'empty') {
    		$roomId = '';
    	}
    	$beds = $bedManagement->getAvailableBedsOfRoom($roomId);
    	
    	// in case patient has no bed, $currentBedId is an empty string ''
    	$currentBedId = $_GET['currentBedId'];
    	$response = "";
    	foreach ($beds as $item) {
    		// the available beds don't have current bed of patient.
    		if ($item['bed_id'] != $currentBedId) {
    		    $option = "<option id='" . $item['bed_id']. "' value='" . $item['bed_id'] . "'>" . $item['code'] . "</option>";
    		    $response = $response . $option;
    		}
    	}
    	return $response;
    }
    
    /**
     * Update the current bed of patient to status 'current=false' (it means patient hasn't stayed in this bed).
     * With new assigned bed, assign this bed for patient at some department.
     * When create new record in table 'patient_bed', insert field 'ticket_id' as the current ticket in department
     * of patient also (to pay money when coming out of hospital).
     * 
     */
    public function actionSaveBedForPatient() {
    	$patientId = $_GET['patientId']; 
    	$deptId = $_GET['deptId'];
    	$ticketId = $_GET['ticketId']; 
    	$newBedId = $_GET['newBedId'];
    	
    	$bedManagement = new BedManagement();
    	// update the current bed
    	$bedManagement->changeBedStatus($patientId, $deptId);
    	    	
    	$bed = Celini::newORDO('Bed', $newBedId);
    	$patBedId = $bedManagement->changeBed($patientId, $deptId, $ticketId, $bed);
    	$this->logger->info("Assigned bed '" . $newBedId . "' for patient '" . $patientId . "'");
    	return $patBedId;
    }
    
    /**
     * View bed status of a specific department.
     */
    public function actionViewBedStatus() {
    	$deptId = $_GET['deptId'];    	
    	$dept = Celini::newORDO('Department')->getDepartment($deptId);
        $deptInfo = L10nHelper::_('Department') . " " . $dept['name'];
            
    	// get list of rooms which has at least one bed
    	$bedManagement = new BedManagement();
        $rooms = $bedManagement->getRoomsAtLeastBed($deptId);
        
        $roomIds = '';
        $index = 0;
        foreach ($rooms as $item) {
        	if ($index == 0) {
        		$roomIds = $item['room_id'];
        	} else {
        		$roomIds .= "," . $item['room_id'];
        	}
        	$index++;
        }
        $beds = $bedManagement->getRoomsWithAvailableBeds($roomIds);
        $emptyBeds = array();
        foreach ($beds as $item) {
        	$roomId = $item['room_id'];
        	$empty = 0;
        	if ($item['max_patients'] > $item['patients']) {
        		$empty = 1;
        	}
        	if (isset($emptyBeds[$roomId])) {
        		$emptyBeds[$roomId] = $emptyBeds[$roomId] + $empty;
        	} else {
        		$emptyBeds[$roomId] = $empty;
        	}
        }
        $index = 0;
        foreach ($rooms as $item) {
        	$roomId = $item['room_id'];
        	if (isset($emptyBeds[$roomId])) {
        		$item['empty_beds'] = $emptyBeds[$roomId];        		        	
        	} else {
        		$item['empty_beds'] = 0;
        	}
        	$rooms[$index] = $item;
        	$index++;
        }
        $this->assign('deptInfo', $deptInfo);
        $this->assign('rooms', $rooms);
        return $this->view->render('bed_status.html');
    }
    
    /**
     * View bed status of a specific room. 
     */
    public function actionGetBedsOfRoom() {
        $roomId = $_GET['roomId'];
        $room = Celini::newORDO('Room')->getRoom($roomId);
        $roomInfo = L10nHelper::_('Room') . " " . $room['room_name'];
            
        // get bed code, max patients, current patients of all beds in $roomId
        $bedMng = new BedManagement();  
        $beds = $bedMng->getNumberOfPatientsForBed($roomId);
        
        $this->assign('roomInfo', $roomInfo);
        $this->assign('beds', $beds);
        $this->assign('roomName', $room['room_name']);
        return $this->view->render('bed_status.html');
    }
}

?>